package com.wuming.house.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.wuming.house.core.JsonResult;
import com.wuming.house.dao.SmsMapper;
import com.wuming.house.model.YanZhenCode;
import com.wuming.house.util.HttpClientUtil;
import com.wuming.house.util.HttpRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xuhuai
 * @createTime 2019/11/19 17:05
 * @Description:
 */
@Controller
@RequestMapping("/api/sms")
public class SmsApi {

    @Autowired
    private SmsMapper smsMapper;

    @ResponseBody
    @RequestMapping("/generate")
    public JsonResult generate(@RequestParam String mobile,@RequestParam(defaultValue = "HOUSE") String type){
        String url = "http://139.224.26.188:9080/sms/generate?mobile="+mobile+"&type="+type;
        HttpClientUtil httpClientUtil = HttpClientUtil.getInstance();
        String res = httpClientUtil.httpGet(url);
        JSONObject jsonObject = JSONObject.parseObject(res);
        jsonObject = jsonObject.getJSONObject("data");
        Long codeId = null;
        if (null!=jsonObject){
            YanZhenCode yanZhenCode = new YanZhenCode();
            yanZhenCode.setCode(jsonObject.getString("code"));
            yanZhenCode.setMobile(jsonObject.getString("mobile"));
            yanZhenCode.setCreateDatetime(jsonObject.getDate("createDatetime"));
            smsMapper.insert(yanZhenCode);
            codeId = yanZhenCode.getId();
        }
        return new JsonResult(codeId,"",true);
    }


    @ResponseBody
    @RequestMapping("/checkCode")
    public JsonResult checkCode(YanZhenCode yanZhenCode){
        YanZhenCode yanZhenCode1 = smsMapper.selectByPrimaryKey(yanZhenCode.getId());
        if (yanZhenCode.getCode().equalsIgnoreCase(yanZhenCode1.getCode())){
            return new JsonResult(null,"验证码输入正确",true);
        }
        return new JsonResult(null,"验证码输入错误",false);
    }
}
